/**
* User services
*/
app.factory("userServices", function($http, toolsServices) {
	console.info("start to register userServices");
	var users = [];
	var profiles = [];
	return {
		getProfiles: function(callback) {
			console.log('userServices - get all profiles');
			if(profiles== undefined || profiles.length == 0) {
				console.log('userServices - have to initialize profiles');
				$http.post(app.servicebase, { "service" : "getProfiles" }).
				success(function(data, status) 
				{
					console.info("getProfiles - call success"); 
					if(data.isFailed == true)
					{
						throw data.exception;
					}
					profiles = data.response;
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.error("getUsers - call failed"); 	
					throw status + ' : ' + data;		
				});
			} else {
				var response = {'isFailed' : false, 'response' : data};
				callback(response);
			}
		},
		allUsers: function(callback) {
			console.log('userServices - get all users');
			$http.post(app.servicebase, { "service" : "getUsers" }).
			success(function(data, status) 
			{
				console.info("getUsers - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				users = data.response;
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("getUsers - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		isLoggedIn : function() {
			return (toolsServices.getCurrentUser() != undefined) ? true : false;
		},
		getLoggedUser: function() {
			return toolsServices.getCurrentUser();
		},
		authenticate: function(callback, login, password) {
			$http.post(app.servicebase, { "service" : "authenticate", "login" : login, "password" : password }).
			success(function(data, status) 
			{
				console.info("userServices.authenticate - call success"); 
				if(data.isFailed == true) {
					console.info("userServices.authenticate - error : " + data.exception); 
				}
				else {
					toolsServices.setCurrentUser(data.response);
				}
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("authenticate - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		memberSubscription: function(callback, firstname, lastname, email, object, message) {
			$http.post(app.servicebase, { "service" : "demandNewMember", "firstname" : firstname, "lastname" : lastname, "email" :  email, "object" : object, "message" : message}).
			success(function(data, status) 
			{
				console.info("addNewMemberCommand - call success"); 	
				callback(data);				
			})
			.
			error(function(data, status) 
			{
				console.error("addNewMemberCommand - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		sendContact: function(callback, firstname, lastname, email, object, message) {
			$http.post(app.servicebase, { "service" : "sendContact", "firstname" : firstname, "lastname" : lastname, "email" :  email, "object" : object, "message" : message}).
			success(function(data, status) 
			{
				console.info("sendContact - call success"); 	
				callback(data);				
			})
			.
			error(function(data, status) 
			{
				console.error("sendContact - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		logout: function(callback) {
			console.info("userServices.logout - call"); 
			toolsServices.removeCurrentUser();
			callback(true);
		},
		add: function(callback, firstname, lastname, company, email, phonenumber, webaddress, profile) {
			console.log('userServices - add new user');
			$http.post(app.servicebase, { "service" : "addUser", "firstname" : firstname, "lastname" : lastname, "company" : company, "email" : email, "phonenumber" : phonenumber, "webaddress" : webaddress, "profile" : profile}).
			success(function(data, status) 
			{
				console.info("addUser - call success"); 	
				users.push(data.response);
				callback(data);				
			})
			.
			error(function(data, status) 
			{
				console.error("addUser - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		update: function(callback, id, firstname, lastname, company, email, phonenumber, webaddress, profile, login) {
			$http.post(app.servicebase, { "service" : "updateUser", "id" : id, "firstname" : firstname, "lastname" : lastname, "company" : company, "email" : email, "phonenumber" : phonenumber, "webaddress" : webaddress, "login" : login, "profile" : profile}).
			success(function(data, status) 
			{
				console.info("updateUser - call success"); 	
				for(var i = 0; i < users.length; i++) {
					var localuser = users[i];
					if(localuser.Id == data.response.Id) {
						users.splice(i, 1);
					}
				}
				users.push(data.response);
				callback(data);				
			})
			.
			error(function(data, status) 
			{
				console.error("updateUser - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		resetUserPassword: function(callback, id) {
			console.log('userServices - update user password');
			$http.post(app.servicebase, { "service" : "resetUserPassword", "id" : id}).
			success(function(data, status) 
			{
				console.info("resetUserPassword - call success"); 	
				callback(data);				
			})
			.
			error(function(data, status) 
			{
				console.error("resetUserPassword - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		attachAvatar: function(callback, id, file) {
			console.info("userServices - attachAvatar called with id user : " + id);
			var fd = new FormData();
			fd.append("service", "attachAvatarOnAnUser");
			fd.append("idUser", id);
			fd.append("file", file);
			$http.post(app.servicebase,
				fd,
				{ 
					withCredentials: true, headers: {'Content-Type': undefined }, transformRequest: angular.identity 
				}
			).
			success(function(data, status) 
			{
				console.info("attachAvatarOnAnUser - call success"); 	
				callback(data);		
			})
			.
			error(function(data, status) 
			{
				console.error("attachAvatarOnAnUser - call failed"); 	
				throw status + ' : ' + data;			
			});
		},
		deleteUser: function(callback, user) {
			console.info("userServices - deleteUser called"); 
			$http.post(app.servicebase, { "service" : "deleteUser", "id" : user.Id }).
			success(function(data, status) 
			{
				console.info("deleteUser - call success"); 
				for(var i = 0; i < users.length; i++) {
					var localuser = users[i];
					if(localuser.Id == user.Id) {
						users.splice(i, 1);
					}
				}
				data.response = users;
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("deleteUser - call failed"); 	
				throw status + ' : ' + data;						
			});
		},
		getUser:  function(callback, id) {
			console.info("userServices - getUser called"); 
			$http.post(app.servicebase, { "service" : "getUser", "id" : id }).
			success(function(data, status) 
			{
				console.info("getUser - call success"); 
				var index = -1;
				for(var i = 0; i < users.length; i++) {
					var localuser = users[i];
					if(localuser.Id == id) {
						index = i;
						users.splice(i, 1);
					}
				}
				users.splice(index, 0, data.response);
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("deleteUser - call failed"); 	
				throw status + ' : ' + data;						
			});
		}
	};
});